package com.ziroom.zifamily.zeus.json;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

/**
 * <p>Json常用工具类</p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年11月24日 17:35
 * @since 1.0
 */
public final class JsonObjectTransform {

    /** 默认格式化日期 */
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /** 默认时区 */
    private static final String DEFAULT_TIME_ZONE = "GMT+8";

    private JsonObjectTransform() {

    }

    /**
     * 字符串转为json
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends Jsonable> T json2Object(String json, Class<T> clazz) {
        //设置全局的时间转化
        SimpleDateFormat smt = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        return json2Object(json, clazz, smt);
    }

    public static <T extends Jsonable> T json2Object(String json, Class<T> clazz, SimpleDateFormat smt) {
        ObjectMapper mapper = new ObjectMapper();
        Jsonable obj = null;
        try {
            //json中多余的参数不报错，不想要可以改掉
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.setDateFormat(smt);
            //解决时区差8小时问题
            mapper.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
            obj = (Jsonable)mapper.readValue(json, clazz);
            return (T) obj;
        } catch (Exception e) {
            throw new RuntimeException("Json转换成Entity时出现异常。", e);
        }
    }

    /**
     * Json转为L
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends Jsonable> List<T> json2List(String json, Class<T> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, new Class[]{clazz});
        try {
            return (List)mapper.readValue(json, javaType);
        } catch (JsonParseException e1) {
            throw new RuntimeException("Json转换成List<T>异常", e1);
        } catch (JsonMappingException e2) {
            throw new RuntimeException("Json转换成List<T>异常", e2);
        } catch (IOException e3) {
            throw new RuntimeException("Json转换成List<T>异常", e3);
        }
    }


    /**
     * 对像转为json
     * @param object
     * @return
     */
    public static String object2Json(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        StringWriter writer = new StringWriter();

        try {
            mapper.writeValue(writer, object);
        } catch (IOException e) {
            throw new RuntimeException("object转换成Json时出现异常", e);
        }
        return writer.toString();
    }

}
